package com.restaurant.serviceimpl;

import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.restaurant.domain.Employee;
import com.restaurant.service.EmployeeService;

public class EmployeeServiceImpl implements EmployeeService {

   private HibernateTemplate hibernateTemplate;

   @Autowired
   public void setSessionFactory(SessionFactory sessionFactory){
       this.hibernateTemplate = new HibernateTemplate(sessionFactory);
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Employee addOrModify( Employee objEmployee ) {
       return ( Employee ) hibernateTemplate.merge( objEmployee );
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Employee deleteRecord( Employee objEmployee ) {
       hibernateTemplate.delete( objEmployee );
       return objEmployee;
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Employee modifyRecord( Employee objEmployee ) {
       return ( Employee ) hibernateTemplate.merge( objEmployee );
   }

   @SuppressWarnings("unchecked")
   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public List<Employee> loadAll() {
       return (List<Employee>)hibernateTemplate.findByNamedQuery( "Employee.findAll");
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Employee loadByPrimaryKey( Long primaryKey ) {
       return (Employee) hibernateTemplate.get( Employee.class, primaryKey );
   }

   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public Employee deleteByPrimaryKey( Long primaryKey ) {
       return deleteRecord(loadByPrimaryKey(primaryKey));
   }

   @SuppressWarnings("unchecked")
   @Override
   @Transactional(propagation = Propagation.REQUIRED, isolation=Isolation.DEFAULT)
   public List<Employee> loadByHql(String query) {
       return ( List<Employee> ) hibernateTemplate.find( query );
   }
}